#+_+ coding:utf-8 +_+

import hashlib
import time
from common import errno
from storage import uid

def CheckSignature(signature,*args):
    items = list()
    for arg in args:
        items.append(str(arg))
    items = sorted(items)
    rst = md5(*items)
    if signature == rst :
        return True
    return False

def IsValiedUID(id):
    rst = uid.UID.GetTicket(id)

    if not rst:
        return False
    else : 
        return True

def IsValied(token):
    """ 
    Test if token is in token cache 

    @param token : token to be check
    @return : True if valied otherwise return False
    """
    return True

def GenUID():
    """ 
    Generate the next uid

    @return : the valied UID    
    """
    return uid.UID.Genrate()

def md5(*args):
    md5Ctx = hashlib.md5()
    for arg in args:
        md5Ctx.update(str(arg))
    return md5Ctx.hexdigest()
    

def GenTicket(uid,token):
    """
    Generate the ticket

    @param uid : user's uid
    @param token : user's token
    @return : user's ticket
    """
    timestamp = time.time()
    return md5(uid,token,timestamp)
